package main

import (
	"context"
	"errors"
	"fmt"
	"io"
	gohttp "net/http"
	"os"
	"os/signal"

	"github.com/go-kratos/kratos/v2/transport/http"
	"golang.org/x/sync/errgroup"
)

func main() {
	g := new(errgroup.Group)
	ctx, cancel := context.WithCancel(context.Background())

	s := http.NewServer()

	g.Go(func() error {
		helloHandler := func(w gohttp.ResponseWriter, req *gohttp.Request) {
			io.WriteString(w, "Hello, world!\n")
		}
		s.HandleFunc("/hello", helloHandler)
		go s.Start(ctx)
		return nil
	})

	g.Go(func() error {
		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Kill)
		<-c
		return errors.New("Got signal Kill")
	})

	if err := g.Wait(); err != nil {
		fmt.Println(err)
		cancel()
	}
}
